Computer Science Related Others Courses AvailableThe Best Codder.blogspot.com

String to List in Python

4 min read

 

String to List in Python

We will use the following methods to meet our objective-

  1. Using split()
  2. Using split() with a separator
  3. Using strip()
  4. Using map()

Let us discuss each one of them.

In the first program, we will make use of split() to convert the string to a list in Python.

Using split()

The program given below illustrates how it can be done.

  1. # Initialising the string values  
  2. str_val1 = "Let us study programming."  
  3. str_val2 = "But before that it is essential to have a basic knowledge of computers."  
  4. str_val3 = "So first study what is IPO cycle."  
  5. str_val4 = "Then learn about the generation of computers."  
  6. #using split()  
  7. print(str_val1.split())  
  8. print(str_val2.split())  
  9. print(str_val3.split())  
  10. print(str_val4.split())  

Output:

['Let', 'us', 'study', 'programming.']
['But', 'before', 'that', 'it', 'is', 'essential', 'to', 'have', 'a', 'basic', 'knowledge', 'of', 'computers.']
['So', 'first', 'study', 'what', 'is', 'IPO', 'cycle.']
['Then', 'learn', 'about', 'the', 'generation', 'of', 'computers.']

Explanation-

  1. In the first step, we have initialized the four strings that we would like to convert.
  2. After this, we have used the split() so that we obtain a list in which each word of a string represents an element of the list.

In the second program, we have specified a separator in split().

Using split() with a Separator

Consider the given program,

  1. # Initializing the string values  
  2. str_val1="Let @ us @ study @ programming."  
  3. str_val2="But # before # that # it # is # essential # to # have # a # basic # knowledge # of # computers."  
  4. str_val3="So $ first $ study $ what $ is $ IPO $ cycle."  
  5. str_val4="Then % learn % about % the % generation % of % computers."  
  6. # Using split()  
  7. print(str_val1.split("@"))  
  8. print(str_val2.split("#"))  
  9. print(str_val3.split("$"))  
  10. print(str_val4.split("%"))  

Output:

['Let ', ' us ', ' study ', ' programming.']
['But ', ' before ', ' that ', ' it ', ' is ', ' essential ', ' to ', ' have ', ' a ', ' basic ', ' knowledge ', ' of ', ' computers.']
['So ', ' first ', ' study ', ' what ', ' is ', ' IPO ', ' cycle.']
['Then ', ' learn ', ' about ', ' the ', ' generation ', ' of ', ' computers.']

Explanation-

The approach is similar to the previous program, the only difference it takes an element in the list whenever a separator occurs.

Using split()

The program given below illustrates how it can be done.

  1. # Initialising the string values  
  2. str_val1 = "Let us study programming."  
  3. str_val2 = "But before that it is essential to have a basic knowledge of computers."  
  4. str_val3 = "So first study what is IPO cycle."  
  5. str_val4 = "Then learn about the generation of computers."  
  6. #using split()  
  7. print(str_val1.split())  
  8. print(str_val2.split())  
  9. print(str_val3.split())  
  10. print(str_val4.split())  

Output:

['Let', 'us', 'study', 'programming.']
['But', 'before', 'that', 'it', 'is', 'essential', 'to', 'have', 'a', 'basic', 'knowledge', 'of', 'computers.']
['So', 'first', 'study', 'what', 'is', 'IPO', 'cycle.']
['Then', 'learn', 'about', 'the', 'generation', 'of', 'computers.']

Explanation-

  1. In the first step, we have initialized the four strings that we would like to convert.
  2. After this, we have used the split() so that we obtain a list in which each word of a string represents an element of the list.

In the second program, we have specified a separator in split().

Using split() with a Separator

Consider the given program,

  1. # Initializing the string values  
  2. str_val1="Let @ us @ study @ programming."  
  3. str_val2="But # before # that # it # is # essential # to # have # a # basic # knowledge # of # computers."  
  4. str_val3="So $ first $ study $ what $ is $ IPO $ cycle."  
  5. str_val4="Then % learn % about % the % generation % of % computers."  
  6. # Using split()  
  7. print(str_val1.split("@"))  
  8. print(str_val2.split("#"))  
  9. print(str_val3.split("$"))  
  10. print(str_val4.split("%"))  

Output:

['Let ', ' us ', ' study ', ' programming.']
['But ', ' before ', ' that ', ' it ', ' is ', ' essential ', ' to ', ' have ', ' a ', ' basic ', ' knowledge ', ' of ', ' computers.']
['So ', ' first ', ' study ', ' what ', ' is ', ' IPO ', ' cycle.']
['Then ', ' learn ', ' about ', ' the ', ' generation ', ' of ', ' computers.']

Explanation-

The approach is similar to the previous program, the only difference it takes an element in the list whenever a separator occurs.

In this program, the separators in the strings were @, #, $ & %.

Now, let's see how to strip() can be used.

Using strip()

The following program illustrates the same-

  1. # Initialising the string values  
  2. str_val1 = "Let us study programming."  
  3. str_val2 = "But before that it is essential to have a basic knowledge of computers."  
  4. # Using list()  
  5. print(list(str_val1.strip()))  
  6. print(list(str_val2.strip()))  

Output:

['L', 'e', 't', ' ', 'u', 's', ' ', 's', 't', 'u', 'd', 'y', ' ', 'p', 'r', 'o', 'g', 'r', 'a', 'm', 'm', 'i', 'n', 'g', '.']
['B', 'u', 't', ' ', 'b', 'e', 'f', 'o', 'r', 'e', ' ', 't', 'h', 'a', 't', ' ', 'i', 't', ' ', 'i', 's', ' ', 'e', 's', 's', 'e', 'n', 't', 'i', 'a', 'l', ' ', 't', 'o', ' ', 'h', 'a', 'v', 'e', ' ', 'a', ' ', 'b', 'a', 's', 'i', 'c', ' ', 'k', 'n', 'o', 'w', 'l', 'e', 'd', 'g', 'e', ' ', 'o', 'f', ' ', 'c', 'o', 'm', 'p', 'u', 't', 'e', 'r', 's', '.']

Explanation-

  1. In the first step, we have initialized the two strings that we would like to convert.
  2. After this, we have used the strip() so that we obtain a list, where each character represents of the string represents an element in the list.

Convert Strings to List of Lists using map()

  1. # Initializing the string values  
  2. str_val1="Let us study programming."  
  3. str_val2="But before that it is essential to have a basic knowledge of computers."  
  4. #using split()  
  5. str_val1 = str_val1.split()  
  6. str_val2 = str_val2.split()  
  7. list_str1 = list(map(list,str_val1))  
  8. list_str2 = list(map(list,str_val2))  
  9. #displaying the list values  
  10. print(list_str1)  
  11. print(list_str2)  

Output:

[['L', 'e', 't'], ['u', 's'], ['s', 't', 'u', 'd', 'y'], ['p', 'r', 'o', 'g', 'r', 'a', 'm', 'm', 'i', 'n', 'g', '.']]
[['B', 'u', 't'], ['b', 'e', 'f', 'o', 'r', 'e'], ['t', 'h', 'a', 't'], ['i', 't'], ['i', 's'], ['e', 's', 's', 'e', 'n', 't', 'i', 'a', 'l'], ['t', 'o'], ['h', 'a', 'v', 'e'], ['a'], ['b', 'a', 's', 'i', 'c'], ['k', 'n', 'o', 'w', 'l', 'e', 'd', 'g', 'e'], ['o', 'f'], ['c', 'o', 'm', 'p', 'u', 't', 'e', 'r', 's', '.']]

Explanation-

  1. In the first step, we have initialized the two strings that we would like to convert.
  2. After this, we have used the split() method followed by map() so that we map the list functionality to each element of the string.
  3. On executing the given program, the desired output is displayed.

Finally, in the last program we have used the string of integers,

Converting String of Integers

Consider the program given below,

  1. #initialising the string values  
  2. str_val1 = "1 2 3 4 5 6 7 8 9"  
  3. str_val2 = "12 21 32 44 54 76 83"  
  4. #using split()  
  5. str_val1 = str_val1.split()  
  6. str_val2 = str_val2.split()  
  7. list_str1 = list(map(int,str_val1))  
  8. list_str2 = list(map(int,str_val2))  
  9. #displaying the list values  
  10. print(list_str1)  
  11. print(list_str2)  

Output:

[1, 2, 3, 4, 5, 6, 7, 8, 9]
[12, 21, 32, 44, 54, 76, 83]

Explanation-

The logic is similar to the above program but here we have passed a string of integers and applied the 'int' functionality on all the elements.

You may like these posts

  •  String to List in PythonWe will use the following methods to meet our objective-Using split()Using split() with a separatorUsing strip()Using map()Le…

Post a Comment

© 2025Python . The Best Codder All rights reserved. Distributed by